﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SimpleServiceBus.EndpointMonitor.Web.Helpers
{

    public static class StringExtensions
    {
        public static string Elide(this string target, int maxLength)
        {
            if (string.IsNullOrEmpty(target) || target.Length <= maxLength)
                return target;
            return target.Substring(0, maxLength) + "...";
        }

        public static bool IsNonEmpty(this string target)
        {
            return target != null && target.Trim() != String.Empty;
        }

        public static string Join<T>(this IEnumerable<T> list, string delimiter, Func<T, string> formatter)
        {
            var values = new List<string>();
            foreach (T obj in list)
            {
                if (obj != null)
                    values.Add(formatter(obj));
            }
            return String.Join(delimiter, values.ToArray());
        }

        public static string Join<T>(this IEnumerable<T> list, string delimiter, string formatString)
        {
            return Join(list, delimiter, (obj) => String.Format(formatString, obj));
        }

        public static string FormatAs<T>(this T target, string format)
        {
            return String.Format(format, target);
        }

        public static string FormatAs(this object[] args, string format)
        {
            return String.Format(format, args.ToArray());
        }

        public static string FormatWith(this string format, params object[] args)
        {
            return String.Format(format, args);
        }
    }

}
